/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Administrator
 */
public abstract class Scene {

    public static final int MESSAGE_NORMAL = 0;
    public static final int MESSAGE_MARRY = 1;
    public static final int MESSAGE_ACCEPT_MARRY = 2;
    public static final int MESSAGE_REJECT_MARRY = 3;
    public static final int MESSAGE_BIRTH = 4;
    public static final int MESSAGE_ACCEPT_BIRTH = 5;
    public static final int MESSAGE_REJECT_BIRTH = 6;
    public static final int MESSAGE_FRIEND = 7;
    public static final int MESSAGE_ACCEPT_FRIEND = 8;
    public static final int MESSAGE_REJECT_FRIEND = 9;
    protected static final int DIALOG_HEIGHT = 40;
    protected static final int LOADING_FRAME = 7;
    protected static final int ROW_HEIGHT = 40;
    private static final int INFO_HEIGHT = 220;
    private static final int TAB_INFO = 0;
    private static final int TAB_ACHIVENEMENT = 1;
    private static final int TYPE_SMS = 6;
    protected static final int SCREEN_PORTRAIT = 0;
    protected static final int SCREEN_LANDSCAPE = 1;
    protected int screenMode;
    protected int topScreen;
    protected int leftScreen;
    protected int canvasHeight;
    protected int canvasWidth;
    private int startMessageIndex;
    protected int numberMessageShow;
    protected int numberMessage;
    private int listMessageIndex;
    protected int messageBoardHeight;
    private int kindOfMessage;
    private int messageIndex;
    public int countFrame;
    public int subMenuBottom;
    private int numberFrame;
    protected int bottomBarHeight;
    protected int loadingIndex;
    private long startFrame;
    private long totalMemory;
    private long useMemory;
    protected int tabSelectedIndex;
    private int tabImageHeight;
    private int tabImageWidth;
    protected int charMoneyIndex;
    //------------------------------------------
    public boolean isShowSubMenu;
    public boolean isLoading;
    public boolean isShowChatBox;
    public boolean isGoBack;
    private boolean isKillClient;
    protected boolean keyPad;
    private boolean isReading;
    protected boolean isShowMessage;
    private boolean isLostConnection;
    protected boolean isShowReply;
    private boolean isDelete;
    protected boolean isShowBox;
    protected boolean isShowUserInfo;
    protected boolean isGetListFriend;
    protected boolean isGetUserInfo;
    protected boolean isShowTableSms;
    protected boolean isWaitingSendSms;
    protected boolean isShowSpeaker;
    //------------------------------------------
    public String questionStringInfo;
    public String dialogString;
    public String questionString;
    protected String issuerName;
    private String cardSerial;
    private String cardCode;
    protected int topUserInfo;
    protected int topUserInfoAnchor;
    protected String[] saveSubMenu;
    protected boolean isSwitchScene;
    protected boolean isShow;
    protected boolean isShowBetBox;
    //-------------------------------------------
    public Image loadingImage;
//    public Image backgroundImage;
    private Image darkBgImage;
    private Image newMessageImage;
    private Image userInfoImage;
    private Image tabImage;
    private Image transparentBackground;
    //------------------------------------------
    public MenuObject subMenu;
    public Library library;
    public MainCanvas parent;
    public GameData gameData;
    protected CustomTextBox replyBox;
    protected CustomTextBox nameChildBox;
    protected CustomTextBox speakerBox;
    protected FlyString speakFlyString;
    protected Vector speakLists;
    protected Dialog dialog;
    protected boolean isGetCardCode;
    protected boolean isGetCardSerial;
    protected String numberSms;
    protected boolean isNeedUpdate;

    public Scene(MainCanvas parent) {
        this.parent = parent;
        gameData = GameData.getInstance();
        checkType();
        initValue();
        initImage();
        initSubMenu();
        initObject();
    }

    private void initObject() {
        speakLists = new Vector();
        speakerBox = new CustomTextBox(PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, false, keyPad);
        speakerBox.setShowTypeInput(false);
        speakFlyString = new FlyString(FlyString.FLY_HORIZONTAL, PiPoDesigner.COLOR_WHITE);
    }

    private void checkType() {
        if ((parent.getGameAction(48) == 0) && (parent.getGameAction(49) == 0) && (parent.getGameAction(50) == 0)
                && (parent.getGameAction(51) == 0) && (parent.getGameAction(52) == 0) && (parent.getGameAction(53) == 0)
                && (parent.getGameAction(54) == 0) && (parent.getGameAction(55) == 0)
                && (parent.getGameAction(56) == 0) && (parent.getGameAction(57) == 0)) {
            keyPad = false;
        } else {
            keyPad = true;
        }
    }

    private void initImage() {
        try {
//            backgroundImage = library.getBackgroundImage(screenMode);
            loadingImage = library.getLoadingImage();
            darkBgImage = library.getDarkBg();
            newMessageImage = Utils.loadImage("new_mail.png");
            transparentBackground = Utils.createTransparentImage(0xdd000000, canvasWidth - 20, canvasHeight - 70);
            tabImage = Utils.loadImage("tab.png");
            tabImageHeight = tabImage.getHeight();
            tabImageWidth = tabImage.getWidth();
            userInfoImage = Utils.loadImage("user_info.png");
        } catch (IOException ex) {
        }
    }

    private void initSubMenu() {
        subMenu = library.getSubMenu();
        setListSubMenu();
    }

    public abstract void setListSubMenu();

    private void initValue() {
        this.canvasHeight = parent.getHeight();
        this.canvasWidth = parent.getWidth();
        screenMode = (canvasWidth < canvasHeight ? SCREEN_PORTRAIT : SCREEN_LANDSCAPE);
        bottomBarHeight = 30;
        dialogString = "";
        questionString = "";
        questionStringInfo = "";
        issuerName = "";
        isGoBack = false;
        library = Library.getInstance(canvasWidth, canvasHeight);
        subMenuBottom = canvasHeight - bottomBarHeight;
        messageBoardHeight = canvasHeight - 40 - bottomBarHeight;
        if (Debug.isTest) {
            numberMessageShow = 4;
            numberMessage = DataTest.numMessages;
            numberMessageShow = numberMessage < messageBoardHeight / ROW_HEIGHT ? numberMessage : messageBoardHeight / ROW_HEIGHT;
        }
        isShow = true;
        if (screenMode == SCREEN_PORTRAIT) {
            topUserInfo = 25 + tabImageHeight + 40;
        } else {
            topUserInfo = 25 + tabImageHeight + 40;
        }

    }

    private void paintInfo(Graphics g) {
        int rowIndex = (tabSelectedIndex == 0 ? 0 : 1);
        int left = leftScreen + 10;
        int top = 25;
        Utils.drawFrame(g, tabImage, left + 30, top, rowIndex, 0, tabImageWidth >> 1, tabImageHeight);
        PiPoDesigner.drawString(g, "Bản thân", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left + 35, top);
        rowIndex = (tabSelectedIndex == 1 ? 0 : 1);
        Utils.drawFrame(g, tabImage, left + 30 + (tabImageWidth >> 1), top, rowIndex, 0, tabImageWidth >> 1, tabImageHeight);
        PiPoDesigner.drawString(g, "Thành tích", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left + 35 + (tabImageWidth >> 1), top);
        rowIndex = (tabSelectedIndex == 2 ? 0 : 1);
        top += tabImageHeight;
        g.drawImage(transparentBackground, left, top, 0);
        top += 10;
        topUserInfoAnchor = top + 20;
        if (tabSelectedIndex == TAB_INFO) {
            g.setClip(0, top, canvasWidth, canvasHeight - 100);
            int topScroll = topUserInfo;
            String userStatus = gameData.userInfoShow.getStatus();
            if (userStatus.length() > 0) {
                PiPoDesigner.drawString(g, "Status : ", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left + 5, topScroll);
                PiPoDesigner.drawString(g, left + 45, topScroll, userStatus, PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, 0, userStatus.length(), canvasWidth - left - 60);
                topScroll += 30;
            }
            //ten
            PiPoDesigner.drawString(g, "Tên : ", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left + 5, topScroll);
            PiPoDesigner.drawString(g, gameData.userInfoShow.getName(), PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, left + 35, topScroll);
            //tien
            topScroll += 30;
            PiPoDesigner.drawString(g, "Tiền : ", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left + 5, topScroll);
            PiPoDesigner.drawString(g, String.valueOf(gameData.userInfoShow.getMoney()), PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, left + 35, topScroll);
            topScroll += 30;
            if (gameData.userInfoShow.getGender() == 0) {
                PiPoDesigner.drawString(g, "Giới tính : Nam", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left + 5, topScroll);
                Utils.drawFrame(g, userInfoImage, left + 80, topScroll - 5, 3, 0, userInfoImage.getWidth() / 5, userInfoImage.getHeight());
            } else {
                PiPoDesigner.drawString(g, "Giới tính : Nữ", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left + 5, topScroll);
                Utils.drawFrame(g, userInfoImage, left + 80, topScroll - 5, 4, 0, userInfoImage.getWidth() / 5, userInfoImage.getHeight());
            }
            topScroll += 30;
            PiPoDesigner.drawString(g, "Quan hệ :", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left + 5, topScroll);
            if (gameData.userInfoShow.getIsMaried() == 0) {
                PiPoDesigner.drawString(g, "Độc thân", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left + 85, topScroll);
                Utils.drawFrame(g, userInfoImage, left + 55, topScroll - 5, 1, 0, userInfoImage.getWidth() / 5, userInfoImage.getHeight());
            } else {
                Utils.drawFrame(g, userInfoImage, left + 55, topScroll - 5, 0, 0, userInfoImage.getWidth() / 5, userInfoImage.getHeight());
                String str = "Đã kết hôn với " + gameData.userInfoShow.getMatName();
                PiPoDesigner.drawString(g, left + 90, topScroll, str, PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, 0, str.length(), canvasWidth - left - 100);
//                Debug.d("n childs :" + gameData.userInfoShow.getNumberChild());
                if (gameData.userInfoShow.getNumberChild() > 0) {
                    for (int i = 0, l = gameData.userInfoShow.getNumberChild(); i < l; i++) {
                        topScroll += 30;
                        String genderType = (gameData.userInfoShow.getChildGender(i) == 0 ? "Nam" : "Nữ");
                        PiPoDesigner.drawString(g, "Con ruột:  " + gameData.userInfoShow.getChildName(i) + "  (" + genderType + ")", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE,
                                left + 5, topScroll);
                    }
                }
            }
            g.setClip(0, 0, canvasWidth, canvasHeight);
            return;
        }

        if (tabSelectedIndex == TAB_ACHIVENEMENT) {
            PiPoDesigner.drawString(g, "Level : " + gameData.userInfoShow.getLevel(), PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left + 5, top);
            PiPoDesigner.drawString(g, "( " + gameData.userInfoShow.levelPercent + " % )", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left + 100, top);
            top += 30;
            PiPoDesigner.drawString(g, "Thắng : " + gameData.userInfoShow.getNumberWin(), PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left + 5, top);
            top += 30;
            PiPoDesigner.drawString(g, "Thua : " + gameData.userInfoShow.getNumberLoses(), PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left + 5, top);
            top += 30;
            return;
        }
    }

    private void paintTableSms(Graphics g) {
        int left = leftScreen + 10;
        int top = 30;
        g.drawImage(transparentBackground, left, top, 0);
        PiPoDesigner.drawString(g, "Nạp tiền", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, canvasWidth / 2 - PiPoDesigner.stringWidth("nap tien") / 2, top + 5);
        left += 10;
        top += 35;
        if (screenMode == SCREEN_PORTRAIT) {
            g.setColor(0xff0000);
            g.fillRect(left - 5, top + 5 + 30 * charMoneyIndex, transparentBackground.getWidth() - 10, 20);
//        PiPoDesigner.drawString(g, "Tin nhắn test 500  : 10000 xu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left, top + 5);
//        top += 30;
            PiPoDesigner.drawString(g, "Tin nhắn " + gameData.getSmsInfo().getCost1() + "  : " + gameData.getSmsInfo().getGameMoney1() + " xu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left, top + 5);
            top += 30;
            PiPoDesigner.drawString(g, "Tin nhắn " + gameData.getSmsInfo().getCost2() + " : " + gameData.getSmsInfo().getGameMoney2() + " xu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left, top + 5);
            top += 30;
            PiPoDesigner.drawString(g, "Tin nhắn " + gameData.getSmsInfo().getCost3() + " : " + gameData.getSmsInfo().getGameMoney3() + " xu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left, top + 5);
            top += 30;
            PiPoDesigner.drawString(g, "Thẻ cào Vinaphone", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left, top + 5);
            top += 30;
            PiPoDesigner.drawString(g, "Thẻ cào Viettel", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left, top + 5);
            top += 30;
            PiPoDesigner.drawString(g, "Thẻ cào Mobiphone", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left, top + 5);
            top += 30;
            PiPoDesigner.drawString(g, "Nạp thẻ = 150% Sms", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_GREEN, left, top + 5);
        } else {
            g.setColor(0xff0000);
            if (charMoneyIndex < 3) {
                g.fillRect(left - 5, top + 5 + 30 * (charMoneyIndex % 3), 150, 20);
            } else {
                g.fillRect(left - 5 + 160, top + 5 + 30 * (charMoneyIndex % 3), 125, 20);
            }
//        PiPoDesigner.drawString(g, "Tin nhắn test 500  : 10000 xu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left, top + 5);
//        top += 30;
            PiPoDesigner.drawString(g, "Tin nhắn 5000  : 25000 xu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left, top + 5);
            PiPoDesigner.drawString(g, "Thẻ cào Vinaphone", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left + 160, top + 5);
            top += 30;
            PiPoDesigner.drawString(g, "Thẻ cào Viettel", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left + 160, top + 5);
            PiPoDesigner.drawString(g, "Tin nhắn 10000 : 50000 xu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left, top + 5);
            top += 30;
            PiPoDesigner.drawString(g, "Tin nhắn 15000 : 75000 xu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left, top + 5);
            PiPoDesigner.drawString(g, "Thẻ cào Mobiphone", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left + 160, top + 5);
            top += 30;
            PiPoDesigner.drawString(g, "Nạp thẻ = 150% Sms", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_GREEN, left, top + 5);
//            PiPoDesigner.drawString(g, "Thẻ cào Vinaphone", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left, top + 5);
//            top += 30;
//            PiPoDesigner.drawString(g, "Thẻ cào Viettel", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left, top + 5);
//            top += 30;
//            PiPoDesigner.drawString(g, "Thẻ cào Mobiphone", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, left, top + 5);
        }

    }

    public void paint(Graphics g) {
        if (isSwitchScene) {
            isShow = false;
            return;
        }
        isShow = true;
        paintBackground(g);
        paintComponents(g);

        if (isShowMessage) {
            paintMessageBoard(g);
        }

        if (isShowUserInfo) {
            paintInfo(g);
        }
        if (isShowReply && replyBox != null) {
            replyBox.paint(g, leftScreen + 5, canvasHeight - bottomBarHeight - 20, canvasWidth - 10, 20);
        }
        if (isShowTableSms) {
            paintTableSms(g);
        }
        if (dialog != null) {
            dialog.paintInput(g, leftScreen + 10, canvasHeight - 5, canvasWidth - 20);
        }

        if (isShowSubMenu) {
            paintSubMenu(g);
        }

        if (isLoading) {
            paintLoading(g);
        }
        if (isShowSpeaker) {
            speakerBox.paint(g, leftScreen + 10, canvasHeight - 45, canvasWidth - 20, 20);
        }
        if (dialogString.length()
                > 0) {
            paintDialog(g);
        }
        if (questionString.length()
                > 0) {
            paintQuestion(g);
        }
        if (gameData.isHasNewMessage) {
            g.drawImage(newMessageImage, canvasWidth - 20, 30, 0);
        }
        if (speakFlyString != null) {
            if (speakFlyString.isShow) {
                speakFlyString.paint(g);
            }
        }
//        paintMemory(g);
        isShow = true;
        return;
    }

    private void paintQuestion(Graphics g) {
        g.setColor(0x000000);
        g.fillRoundRect(10, canvasHeight - DIALOG_HEIGHT - 2, canvasWidth - 20, DIALOG_HEIGHT, 10, 10);
        int stringDialogWidth = PiPoDesigner.stringWidth(questionString);
        PiPoDesigner.drawString(g, questionString, PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE,
                canvasWidth / 2 - stringDialogWidth / 2, canvasHeight - DIALOG_HEIGHT);
        g.setColor(0xffffff);
        g.fillRect(15, canvasHeight - DIALOG_HEIGHT / 2, canvasWidth - 30, 15);
        PiPoDesigner.drawCenterString(g, "Có", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, 25, canvasHeight - 20);
        PiPoDesigner.drawCenterString(g, "Không", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, canvasWidth - 40, canvasHeight - 20);
        g.drawRoundRect(10, canvasHeight - DIALOG_HEIGHT - 2, canvasWidth - 20, DIALOG_HEIGHT, 10, 10);
    }

    private void paintDialog(Graphics g) {
        int stringDialogWidth = PiPoDesigner.stringWidth(dialogString);
        if (stringDialogWidth < canvasWidth - 30) {
            g.setColor(0x000000);
            g.fillRoundRect(10, canvasHeight - DIALOG_HEIGHT - 2, canvasWidth - 20, DIALOG_HEIGHT, 10, 10);
            PiPoDesigner.drawString(g, dialogString, PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE,
                    canvasWidth / 2 - stringDialogWidth / 2, canvasHeight - DIALOG_HEIGHT);
            g.setColor(0xffffff);
            g.fillRect(15, canvasHeight - DIALOG_HEIGHT / 2, canvasWidth - 30, 15);
            PiPoDesigner.drawCenterString(g, "YES", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, canvasWidth / 2, canvasHeight - 20);
            g.drawRoundRect(10, canvasHeight - DIALOG_HEIGHT - 2, canvasWidth - 20, DIALOG_HEIGHT, 10, 10);
        } else {
            g.setColor(0x000000);
            g.fillRoundRect(10, canvasHeight - DIALOG_HEIGHT * 2 - 2, canvasWidth - 20, DIALOG_HEIGHT * 2, 10, 10);
            PiPoDesigner.drawString(g, 15, canvasHeight - DIALOG_HEIGHT * 2,
                    dialogString, PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, 0, dialogString.length(), canvasWidth - 30);
            g.setColor(0xffffff);
            g.fillRect(15, canvasHeight - DIALOG_HEIGHT / 2, canvasWidth - 30, 15);
            PiPoDesigner.drawString(g, "YES", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, canvasWidth / 2, canvasHeight - 20);
            g.drawRoundRect(10, canvasHeight - DIALOG_HEIGHT * 2 - 2, canvasWidth - 20, DIALOG_HEIGHT * 2, 10, 10);
        }


    }

    private void paintMemory(Graphics g) {
        String platform = System.getProperty("microedition.platform");
//        PiPoDesigner.drawString(g, "frame :" + numberFrame, PiPoDesigner.COLOR_BLACK,
//                PiPoDesigner.COLOR_YELOW, 10, 80);
//        PiPoDesigner.drawString(g, String.valueOf(useMemory) + "/" + String.valueOf(totalMemory), PiPoDesigner.COLOR_BLACK,
//                PiPoDesigner.COLOR_YELOW, 10, 100);
        PiPoDesigner.drawString(g, "device:" + platform, PiPoDesigner.COLOR_BLACK,
                PiPoDesigner.COLOR_YELOW, 10, 80);
    }

    private void paintLoading(Graphics g) {
        g.setColor(0x000000);
        g.fillRoundRect(canvasWidth / 2 - 75, canvasHeight - 45, 150, 40, 10, 10);
        g.setColor(0xffffff);
        g.drawRoundRect(canvasWidth / 2 - 75, canvasHeight - 45, 150, 40, 10, 10);
//        g.drawImage(loadingImage[loadingIndex], canvasWidth / 2 - loadingImage[0].getWidth() / 2,
//                canvasHeight - 40, 0);
        Utils.drawFrame(g, loadingImage, canvasWidth / 2 - loadingImage.getWidth() / 14,
                canvasHeight - 40, loadingIndex, 0, loadingImage.getWidth() / 7, loadingImage.getHeight());
        PiPoDesigner.drawString(g, "Xin chờ", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW,
                canvasWidth / 2 - PiPoDesigner.stringWidth("xin cho") / 2, canvasHeight - 20);

    }

    private void paintMessageBoard(Graphics g) {
        int left = 20;
        int top = 35;
        g.setColor(0x00a2e8);
        g.fillRoundRect(left, top, canvasWidth - 40, messageBoardHeight, 7, 7);
        g.setColor(0xeeeeaa);
        g.fillRoundRect(left + 10, top + 20, canvasWidth - 60, canvasHeight - 70 - bottomBarHeight, 7, 7);
        PiPoDesigner.drawString(g, "Tin nhắn", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, canvasWidth / 2 - PiPoDesigner.stringWidth("tin nhan ") / 2, top + 5);
        if (!isReading) {
            if (numberMessage > 0) {
                paintListMessage(g, left + 5, top + 25);
            } else {
                PiPoDesigner.drawString(g, "Bạn không có tin nhắn nào.", PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_WHITE, leftScreen + 40, top + 25);
            }
        } else {
            int order = numberMessage - (messageIndex + startMessageIndex) - 1;
            UserMessage message = gameData.userMessage[order];
            PiPoDesigner.drawString(g, message.getUserSend(), PiPoDesigner.COLOR_BLACK,
                    PiPoDesigner.COLOR_PINK, left + 15, top + 20);
//            PiPoDesigner.drawString(g, gameData.userMessage[indexSelected].getMessage(), PiPoDesigner.COLOR_BLACK,
//                    PiPoDesigner.COLOR_WHITE, leftScreen + 10, topBoardInfo + 25,canvasWidth-10);
            PiPoDesigner.drawString(g, left + 15, top + 35, message.getMessage(), PiPoDesigner.COLOR_BLACK,
                    PiPoDesigner.COLOR_WHITE, 0, message.getMessage().length(), canvasWidth - 60);
            if (isShowBox && nameChildBox != null) {
                nameChildBox.paint(g, left + 20, top + 120, canvasWidth - 65, 20);
            }
        }
    }

    protected void updateListMessage() {
        if (startMessageIndex + (messageBoardHeight / ROW_HEIGHT) >= numberMessage) {
            numberMessageShow = numberMessage - startMessageIndex;
        }
        parent.repaint();
    }

    private void paintListMessage(Graphics g, int left, int top) {
        int topList = top;
        g.setColor(0x00a2e8);
        g.fillRect(left, top + ROW_HEIGHT * messageIndex, canvasWidth - 60, ROW_HEIGHT);
        for (int i = startMessageIndex; i < numberMessageShow + startMessageIndex; i++) {
            int order = numberMessage - i - 1;
            try {
                PiPoDesigner.drawString(g, gameData.userMessage[order].getUserSend(), PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_PINK, left + 10, topList);
            } catch (Exception ex) {
                Debug.d("out :" + order);
                Debug.d("i :" + i);
            }
            String message = gameData.userMessage[order].getMessage();
            if (PiPoDesigner.stringWidth(message) < canvasWidth - 60) {
                PiPoDesigner.drawString(g, gameData.userMessage[order].getMessage(), PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_WHITE, left + 10, topList + 18);
            } else {
                g.setClip(left, topList + 18, canvasWidth - 70, 17);
                PiPoDesigner.drawString(g, gameData.userMessage[order].getMessage(), PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_WHITE, left + 10, topList + 18);
                g.setClip(0, 0, canvasWidth, canvasHeight);
                PiPoDesigner.drawString(g, "...", PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_WHITE, left + canvasWidth - 70, topList + 18);
            }
            topList += ROW_HEIGHT;
        }
    }

    private void paintSubMenu(Graphics g) {
        subMenu.paint(g, leftScreen, subMenuBottom);
    }

    private void paintBackground(Graphics g) {
        g.setColor(0xffffffff);
        g.fillRect(topScreen, leftScreen, canvasWidth, canvasHeight);
//        g.drawImage(backgroundImage, leftScreen, topScreen, 0);
        g.setColor(0x880000);
        g.fillRect(leftScreen, topScreen, canvasWidth, canvasHeight);
        g.setColor(0xffffff);
        if (screenMode == SCREEN_PORTRAIT) {
            g.drawImage(darkBgImage, leftScreen, topScreen, 0);
        }
    }

    protected void paintBarTextBox(Graphics g) {
        PiPoDesigner.drawString(g, "Đóng", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 5, canvasHeight - 20);
        PiPoDesigner.drawString(g, "Xóa", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth - 30, canvasHeight - 20);
        PiPoDesigner.drawString(g, "Gửi", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth / 2 - 20, canvasHeight - 20);
    }

    protected void paintBarMenu(Graphics g) {
        if (isShowTableSms) {
            PiPoDesigner.drawString(g, "Đóng", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth - 40, canvasHeight - 25);
            return;
        }
        PiPoDesigner.drawString(g, "Menu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 5, canvasHeight - 25);
        if (isShowSubMenu) {
            PiPoDesigner.drawString(g, "Đóng", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth - 40, canvasHeight - 25);
        } else {
            PiPoDesigner.drawString(g, "Thoát", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth - 40, canvasHeight - 25);
        }
    }
//-------------------------------------------------------------------------------------------------------

    public void doThreadAction() {
        countFrame = (++countFrame) % 1024;
        if (isLoading && countFrame % 4 == 0) {
            loadingIndex = (++loadingIndex) % LOADING_FRAME;
            parent.repaint();
        }
        doLoop();
        caculateMemory();
        long currentTime = System.currentTimeMillis();
        if (currentTime - startFrame > 1000) {
            numberFrame = countFrame;
            countFrame = 0;
            startFrame = currentTime;
        }

    }

    public void caculateMemory() {
        totalMemory = Runtime.getRuntime().totalMemory() >> 10;
        useMemory = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 10;
        if (useMemory > 1000) {
            Runtime.getRuntime().gc();
        }

    }

    public void doLoop() {
        if (isShowSpeaker) {
            speakerBox.run();
            parent.repaint();
        }

        if (speakFlyString != null) {
            if (speakFlyString.isShow) {
                speakFlyString.run();
                parent.repaint();
            }
        }
        if (speakLists != null) {
            if (!speakLists.isEmpty()) {
                if (speakFlyString != null && !speakFlyString.isShow) {
                    String speak = (String) speakLists.elementAt(0);
                    speakFlyString.setText(speak, 3, canvasWidth, topScreen + 5);
                    speakFlyString.show(10000);
                    speakFlyString.setLimitHorizontal(canvasWidth);
                    speakLists.removeElementAt(0);
                }
            }
        }
//        if (isWaitingSendSms && isLoading) {
////            Debug.d("isLoading :" + isLoading);
//            if (SmsSender.getInstance().sentResult == 1) {
//                dialogString = "Tin nhắn đã được gửi thành công";
//                isWaitingSendSms = false;
//                isLoading = false;
//                parent.repaint();
//            } else if (SmsSender.getInstance().sentResult == 0) {
//                dialogString = "Tin nhắn không được gửi thành công";
//                isWaitingSendSms = false;
//                isLoading = false;
//                parent.repaint();
//            }
//        }
        if (dialog != null) {
            if (dialog.isShow) {
                dialog.run();
                parent.repaint();
            }
        }
    }
//------------------------------------------------------------------------------

    public void moveLeftInfo() {
        tabSelectedIndex--;
        if (tabSelectedIndex < 0) {
            tabSelectedIndex = 1;
        }
        parent.repaint();
    }

    public void moveRightInfo() {
        tabSelectedIndex = (++tabSelectedIndex) % 2;
        parent.repaint();
    }

    public abstract void paintComponents(Graphics g);

    public abstract void doKeyLeft();

    public abstract void doKeyRight();

    public abstract void doKeyPound();

    public void doKeyUp() {
        if (isShowUserInfo && screenMode == SCREEN_LANDSCAPE) {
            topUserInfo = (topUserInfo < topUserInfoAnchor - 10 ? topUserInfo + 10 : topUserInfoAnchor - 10);
            parent.repaint();
            return;
        }
        if (isShowTableSms) {
            charMoneyIndex--;
            if (charMoneyIndex < 0) {
                charMoneyIndex = TYPE_SMS - 1;
            }
            parent.repaint();
            return;
        }

        if (isShowSubMenu) {
            subMenu.doKeyUp();
            parent.repaint();
            return;
        }
        if (isReading) {
            return;
        }
        if (isShowMessage) {
            moveUpMessage();
            return;
        }

        doKeyUpRemain();

    }

    private void moveUpMessage() {
        if (numberMessage <= 0) {
            return;
        }
        messageIndex--;
        if (messageIndex < 0) {
            numberMessageShow = (messageBoardHeight / ROW_HEIGHT);
            startMessageIndex -= numberMessageShow;
            if (startMessageIndex < 0) {
                startMessageIndex = numberMessage / numberMessageShow;
                startMessageIndex = startMessageIndex * numberMessageShow;
                if (startMessageIndex == numberMessage) {
                    if (numberMessage != 0) {
                        startMessageIndex -= numberMessageShow;
                    } else {
                        startMessageIndex = 0;
                    }
                }
                numberMessageShow = numberMessage - startMessageIndex;
                Debug.d("num show :" + numberMessageShow);
                Debug.d("num start :" + startMessageIndex);
            }
            messageIndex = numberMessageShow - 1;

        }
        parent.repaint();
    }

    protected abstract void doKeyUpRemain();

    private void moveDownMessage() {
        if (numberMessage <= 0) {
            return;
        }
        messageIndex = (++messageIndex) % numberMessageShow;
        if (messageIndex == 0) {
            startMessageIndex += numberMessageShow;
//            Debug.d("start index :" + startIndex);
            messageIndex = 0;
            if (startMessageIndex + numberMessageShow >= numberMessage) {
                numberMessageShow = numberMessage - startMessageIndex;
            }
        }
        if (startMessageIndex + messageIndex == numberMessage) {
            startMessageIndex = 0;
            Debug.d("start index :" + 0);
            messageIndex = 0;
            numberMessageShow = numberMessage < messageBoardHeight / ROW_HEIGHT ? numberMessage : messageBoardHeight / ROW_HEIGHT;

        }
        Debug.d("start index :" + startMessageIndex);
        Debug.d("n show :" + numberMessageShow);
        Debug.d("n message :" + numberMessage);
        parent.repaint();
    }

    public void doKeyDown() {
        if (isShowUserInfo && screenMode == SCREEN_LANDSCAPE) {
            if (topUserInfo > topUserInfoAnchor - 100) {
                topUserInfo -= 10;
                parent.repaint();
            }
            return;
        }
        if (isShowTableSms) {
            if (screenMode == SCREEN_PORTRAIT) {
                charMoneyIndex = (++charMoneyIndex) % TYPE_SMS;
                parent.repaint();
            } else {
                charMoneyIndex = (++charMoneyIndex) % TYPE_SMS;
                parent.repaint();
            }
            return;
        }

        if (isShowSubMenu) {
            subMenu.doKeyDown();
            parent.repaint();
            return;
        }
        if (isReading) {
            return;
        }
        if (isShowMessage) {
            moveDownMessage();
            return;
        }

        doKeyDownRemain();
    }

    protected abstract void doKeyDownRemain();

    public void doSoftKeyLeft() {
        if (isLoading || isShowBox || dialogString.length() > 0) {
            return;
        }
        if (isShowSpeaker) {
            isShowSpeaker = false;
            parent.repaint();
            return;
        }
        if (isShowReply) {
            isShowReply = false;
            isShowBox = false;
            parent.repaint();
            return;
        }
        //  Debug.d("parent call");
        if (questionString.length() > 0) {
            questionString = "";
            reponseQuestion(1);
            dialogString = questionStringInfo;
            parent.repaint();
            return;
        }
        if (!isShowSubMenu && !isShowChatBox && !isShowBetBox) {
            Debug.d("show menu");
            isShowSubMenu = true;
            subMenu.setVisible(true);
            parent.repaint();
            return;
        }
        doSoftKeyLeftRemain();
    }

    protected abstract void doSoftKeyLeftRemain();

    public void doSoftKeyRight() {
        if (dialog != null) {
            if (dialog.isShow) {
                dialog.doSoftKeyRight();
                return;
            }
        }
        if (isLoading || dialogString.length() > 0) {
            return;
        }
        if (isShowSpeaker) {
            speakerBox.doSoftKeyRight();
            parent.repaint();
            return;
        }
        if (isShowTableSms) {
            isShowTableSms = false;
            parent.repaint();
            return;
        }
        if (isLoading) {
            return;
        }
        if (isShowBox) {
            nameChildBox.doSoftKeyRight();
            parent.repaint();
            return;
        }
        if (isShowReply) {
            replyBox.doSoftKeyRight();
            parent.repaint();
            return;
        }
        if (isShowSubMenu) {
            isShowSubMenu = false;
            subMenu.setVisible(false);
            parent.repaint();
            return;
        }
        if (questionString.length() > 0) {
            questionString = "";
            parent.repaint();
            reponseQuestion(0);
            return;
        }
        if (isReading) {
            if (isDelete) {
                deleteMessage();
            }
            isReading = false;
            isDelete = false;
            parent.repaint();
            return;
        }

        if (isShowMessage) {
            isShowMessage = false;
            subMenu.setList(saveSubMenu);
            parent.repaint();
            return;

        }

        if (isShowUserInfo) {
            isGetUserInfo = false;
            isShowUserInfo = false;
            subMenu.setList(saveSubMenu);
            parent.repaint();
            return;
        }
        doSoftKeyRightRemain();
    }

    protected abstract void doSoftKeyRightRemain();

    protected abstract void reponseQuestion(int result);

    protected void doTableSms() {
        numberSms = "";
        if (charMoneyIndex > 2) {
            if (dialog == null) {
                dialog = new Dialog(0, keyPad);
            }
        }
        switch (charMoneyIndex) {
            case 0:
                isWaitingSendSms = true;
                numberSms = gameData.getSmsInfo().getNumberCharge1();
                isLoading = true;
//                    Utils.sendSmsNap(number, "KSNAP " + gameData.myName);
                Request.checkSms();
                break;

            case 1:
                isWaitingSendSms = true;
                numberSms = gameData.getSmsInfo().getNumberCharge2();
                isLoading = true;
                Request.checkSms();
//                    Utils.sendSmsNap(numberSms, "KSNAP " + gameData.myName);
                break;
            case 2:
                isWaitingSendSms = true;
                numberSms = gameData.getSmsInfo().getNumberCharge3();
                isLoading = true;
                Request.checkSms();
//                    Utils.sendSmsNap(numberSms, "KSNAP " + gameData.myName);
                break;
            case 3:
                //vina
                isGetCardCode = true;
                issuerName = "VINA";
                dialog.setString("Mã thẻ");
                dialog.show(true);
                break;
            case 4:
                //viettel
                isGetCardCode = true;
                issuerName = "VT";
                dialog.setString("Mã thẻ");
                dialog.show(true);
                break;
            case 5:
                //mobi
                isGetCardCode = true;
                issuerName = "MOBI";
                dialog.setString("Mã thẻ");
                dialog.show(true);
                break;
        }
        isShowTableSms = false;
        parent.repaint();
    }

    public void doKeyCenter() {
        if (isShowTableSms) {
            doTableSms();
            return;
        }
        if (dialog != null && (isGetCardCode || isGetCardSerial)) {
            if (dialog.isShow) {
                if (isGetCardCode) {
                    isGetCardCode = false;
                    if (dialog.getText().length() > 0) {
                        isGetCardSerial = true;
                        dialog.setString("Serial thẻ");
                        cardCode = dialog.getText();
                    } else {
                        issuerName = "";
                        dialog.show(false);
                    }
                    dialog.setTextInBox("");
                    parent.repaint();
                    return;
                }
                if (isGetCardSerial) {
                    isGetCardSerial = false;
                    if (dialog.getText().length() > 0) {
                        isGetCardSerial = true;
                        cardSerial = dialog.getText();
                        Request.useCard(issuerName, cardCode, cardSerial);

                        isLoading = true;
                    }
                    issuerName = "";
                }
                dialog.show(false);
                parent.repaint();
                return;
            }
        }

        if (isShowReply) {
            String text = replyBox.getText();
            if (text != null) {
                if (isShowMessage) {
                    int order = numberMessage - messageIndex - startMessageIndex - 1;
                    Request.sendMessage(MESSAGE_NORMAL, gameData.userMessage[order].getUserSend(), text);
                    dialogString = "Tin nhắn của bạn đã được gửi";
                    isShowReply = false;
                    parent.repaint();
                    return;
                }
                if (isShowUserInfo) {
                    gameData.userInfoShow.setStatus(text);
                    Request.setStatus(text);
                    isShowReply = false;
                    parent.repaint();
                    return;
                }
            }
            return;
        }

        if (isKillClient
                || isLostConnection) {
            showLogin();
            return;
        }


        if (dialogString.length()
                > 0) {
            dialogString = "";

            parent.repaint();
            return;
        }

        if (isShowBox) {
            String childName = nameChildBox.getText();
            Request.nameChild(childName);
            dialogString = "Con của bạn tên là " + childName;
            isShowBox = false;
            parent.repaint();
            return;
        }

        if (isShowSubMenu) {
            doSubMenu();
            return;
        }

        if (isShowMessage) {
            doMessageBoard();
            return;
        }

        if (isShowSpeaker) {
            isShowSpeaker = false;
            String text = speakerBox.getText();
            if (text.length() > 0) {
                dialogString = "Lời nói của bạn đã được gửi đi";
                Request.speak(text);
            }
            parent.repaint();
            return;
        }

        doKeyCenterRemain();


        return;
    }

    protected abstract void doKeyCenterRemain();

    public abstract void inputKeyCode(int key);

    public void doTouchPressed(int x, int y) {
        Debug.d("x:" + x + ",y:" + y);
        if (y > canvasHeight - 30) {
            touchMenuBar(x, y);
            return;
        }
        if (isShowSubMenu) {
            touchSubMenu(x, y);
            return;
        }
        touchPressedRemain(x, y);
    }

    protected abstract void touchPressedRemain(int x, int y);

    public abstract void doTouchReleased(int x, int y);

    public abstract void doTouchDraged(int x, int y);

    public void processMessage(String[] part) {
        if (part[0].equals("KILL")) {
            dialogString = "Có người đã đăng nhập nick của bạn";
            isKillClient = true;
            parent.repaint();
            return;
        }
        if (part[0].equals("QUIT") && !isKillClient) {
            dialogString = "Không có kết nối tới máy chủ";
            isLostConnection = true;
            parent.repaint();
            return;
        }
        if (part[0].equals("PING")) {
            Request.ping();
            return;
        }
        if (part[0].equals("NMSG")) {
            if (gameData.userMessage != null) {
                UserMessage newMessage = new UserMessage();
                newMessage.setUserSend(part[1]);
                newMessage.setKind(Integer.parseInt(part[2]));
                newMessage.setMessage(part[3]);
                gameData.addMessage(newMessage, numberMessage);
                numberMessage++;
                gameData.numberMessage = numberMessage;
                gameData.isHasNewMessage = true;
                if (isShowMessage) {
                    updateListMessage();
                }
            } else {
                gameData.isHasNewMessage = true;
            }
            parent.repaint();
            return;
        }
        if (part[0].equals("LMSG")) {
            if (part[1].equals("1")) {
                numberMessage = Integer.parseInt(part[2]);
                numberMessage = (numberMessage > GameData.MAX_MESSAGE ? GameData.MAX_MESSAGE : numberMessage);
                gameData.initListMessage(numberMessage);
                gameData.numberMessage = numberMessage;
                listMessageIndex = 0;
                return;
            }
            if (part[1].equals("-1")) {
//                showListMessage();
                numberMessage = gameData.numberMessage;
                gameData.isHasNewMessage = false;
                saveSubMenu = subMenu.getList();
                subMenu.setList(new String[]{"Xóa", "Xóa hết", "Trả lời"});
                messageIndex = 0;
                isLoading = false;
                isShowMessage = true;
                numberMessageShow = numberMessage < messageBoardHeight / ROW_HEIGHT ? numberMessage : messageBoardHeight / ROW_HEIGHT;
//                Debug.d("number show :" + numberMessageShow);
                parent.repaint();
                return;
            }
            if (numberMessage > 0) {
                if (listMessageIndex < GameData.MAX_MESSAGE) {
                    UserMessage userMessage = new UserMessage();
                    userMessage.setUserSend(part[1]);
                    userMessage.setKind(Integer.parseInt(part[2]));
                    userMessage.setMessage(part[3]);
                    userMessage.isRead = Integer.parseInt(part[4]);
                    gameData.addMessage(userMessage, listMessageIndex);
                    listMessageIndex++;
                }
                return;
            }

        }

        if (part[0].equals("INFO") && (isGetUserInfo || isGetListFriend)) {
            PlayerInfo playerInfo = new PlayerInfo(part[1], Long.parseLong(part[2]), Integer.parseInt(part[3]),
                    Integer.parseInt(part[4]), Integer.parseInt(part[5]), Integer.parseInt(part[6]), part[7]);
            playerInfo.setLevelComplete(Integer.parseInt(part[9]));
            gameData.setInfoShow(playerInfo);
            return;
        }
        if (part[0].equals("FAMILY")) {
            //family/isMaried/ten1/ten2/socon/ten/gt
            int isMaried = Integer.parseInt(part[1]);
            int numberChild = 0;
            gameData.setMarryInfo(isMaried);
            if (isMaried == 1) {
                String mateName = (gameData.userInfoShow.getName().equals(part[2]) ? part[3] : part[2]);
                Debug.d("matname :" + mateName);
                gameData.userInfoShow.setMateName(mateName);
                numberChild = Integer.parseInt(part[4]);
                if (numberChild > 0) {
                    int i = 0;
                    do {
                        gameData.setChildNameInfo(part[4 + i + 1]);
                        gameData.setChildGenderInfo(Integer.parseInt(part[4 + i + 2]));
                        i += 2;
                    } while (i < numberChild << 1);
                }
            }

            if (!isGetListFriend) {
                saveSubMenu = subMenu.getList();
                if (isMaried == 1 && numberChild < 2) {
                    subMenu.setList(new String[]{"Status", "Đề nghị sinh con"});
                } else {
                    subMenu.setList(new String[]{"Status"});
                }
                isLoading = false;
                isGetUserInfo = false;
                isShowUserInfo = true;
                parent.repaint();
            }
            return;
        }
        if (part[0].equals("SPEAK")) {
            if (!part[1].equals("0")) {
                String speakString = part[1] + ": " + part[2];
                speakLists.addElement(speakString);
//                speakFlyString.setText(speakString, 3, canvasWidth, topScreen + 5);
//                speakFlyString.show(10000);
//                speakFlyString.setLimitHorizontal(canvasWidth);
            } else {
                dialogString = "Bạn cần item loa để nói";
            }
            parent.repaint();

            return;
        }
        if (part[0].equals("USECARD")) {
            dialogString = part[1];
            isLoading = false;
            parent.repaint();
            return;
        }
        if (part[0].equals("CHECKSMS")) {
            int result = Integer.parseInt(part[1]);
            if (result == 0) {
                dialogString = "Vui lòng thao tác sau 2 phút nữa .";
            } else if (result == 2) {
                dialogString = "Bạn đã nạp tối đa 150K SMS trong ngày. Vui lòng chọn hình thức nạp thẻ";
            } else {
                Utils.sendSmsNap(numberSms, gameData.getSmsInfo().getSynTax() + " " + gameData.myName);
            }
            isLoading = false;
            parent.repaint();
            return;
        }

    }

    public boolean isTouch(int x, int y, int width, int height, int touchX, int touchY) {
        if (x < touchX && touchX < x + width && y < touchY && touchY < y + height) {
            return true;
        }
        return false;
    }

    public void touchMenuBar(int x, int y) {
        if (isTouch(leftScreen, canvasHeight - 30, 60, 100, x, y)) {
            Debug.d("do soft key left");
            doSoftKeyLeft();
            return;
        }
        if (isTouch(canvasWidth - 60, canvasHeight - 30, 70, 100, x, y)) {
            Debug.d("do soft key right");
            doSoftKeyRight();
            if (isShowChatBox) {
                inputKeyCode(8);
            }
            return;
        }
        if (isTouch(canvasWidth / 2 - 40, canvasHeight - 30, 100, 100, x, y)) {
            Debug.d("do center key");
            doKeyCenter();
            return;
        }
    }

    public void touchSubMenu(int x, int y) {
        if (subMenu.isTouch(x, y)) {
            isShowSubMenu = false;
            doSubMenu();
        }
    }

    protected abstract void doSubMenu();

    private void doMessageBoard() {
        if (numberMessage <= 0) {
            return;
        }
        if (!isReading) {
            readMessage();
            return;
        }

    }
//--------------------------------------------------------------------------------

    private void readMessage() {
        int order = numberMessage - messageIndex - startMessageIndex - 1;
        kindOfMessage = gameData.userMessage[order].getKind();
        isReading = true;
        parent.repaint();
        if (kindOfMessage == MESSAGE_FRIEND) {
            questionString = "Bạn có đồng ý kết bạn không ?";
//            isDelete = true;
            parent.repaint();
            return;
        }
        if (kindOfMessage == MESSAGE_MARRY) {
            questionString = "Bạn có đồng ý kết hôn không ?";
//            isDelete = true;
            gameData.isWaitingMarry = false;
            parent.repaint();
            return;
        }
        if (kindOfMessage == MESSAGE_BIRTH) {
            questionString = "Bạn có đồng ý sinh con không ?";
//            isDelete = true;
            parent.repaint();
            return;
        }
        if (kindOfMessage == MESSAGE_ACCEPT_BIRTH) {
            isShowBox = true;
            if (nameChildBox == null) {
                nameChildBox = new CustomTextBox(PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, false, keyPad);
                nameChildBox.setShowTypeInput(false);
            }
            nameChildBox.setFocus(true);
            dialogString = " Hãy đặt tên con của bạn.";
//            isDelete = true;
            parent.repaint();
            return;
        }

    }

    protected void responseMarry(int result) {
        int order = numberMessage - messageIndex - startMessageIndex - 1;
        switch (kindOfMessage) {
            case MESSAGE_MARRY:
                if (result == 1) {
                    Request.sendMessage(MESSAGE_ACCEPT_MARRY, gameData.userMessage[order].getUserSend(), "0");
                    questionStringInfo = "Bạn đã đồng ý kết hôn";
                    isReading = false;
                    parent.repaint();
                } else {
                    isDelete = true;
                    Request.sendMessage(MESSAGE_REJECT_MARRY, gameData.userMessage[order].getUserSend(), "0");
                }
                break;

            case MESSAGE_BIRTH:
                isDelete = true;
                if (result == 1) {
                    Request.sendMessage(MESSAGE_ACCEPT_BIRTH, gameData.userMessage[order].getUserSend(), "0");
                    questionStringInfo = "Bạn đã đồng ý sinh con ";
                    parent.repaint();
                } else {
                    Request.sendMessage(MESSAGE_REJECT_BIRTH, gameData.userMessage[order].getUserSend(), "0");
                }
                break;
            case MESSAGE_FRIEND:
                isDelete = true;
                if (result == 1) {
                    Request.sendMessage(MESSAGE_ACCEPT_FRIEND, gameData.userMessage[order].getUserSend(), "0");
                    questionStringInfo = "Bạn đã đồng ý kết bạn ";
                    parent.repaint();
                } else {
                    Request.sendMessage(MESSAGE_REJECT_FRIEND, gameData.userMessage[order].getUserSend(), "0");
                    questionStringInfo = "Bạn đã từ chối kết bạn ";
                    parent.repaint();
                }
                break;
        }
    }

    private void deleteMessage() {
        int order = numberMessage - messageIndex - startMessageIndex - 1;
        gameData.deleteMessage(order, numberMessage);
        Request.deletedMessage(order);
        numberMessage--;
        gameData.numberMessage = numberMessage;
        isReading = false;
        updateListMessage();
        moveUpMessage();
    }

    protected void doMenuMessage() {
        isShowSubMenu = false;
        parent.repaint();
        if (numberMessage <= 0) {
            return;
        }
        switch (subMenu.getSelectedIndex()) {
            case 0:
                //xoa
                deleteMessage();//                updateListMessage();
                break;
            case 1:
                //xoa het
                int[] id = new int[numberMessage];
                for (int i = 0; i < numberMessage; i++) {
                    id[i] = i;
                    gameData.userMessage[i] = null;
                }
                Request.deletedAllMessage(numberMessage, id);
                numberMessage = 0;
                numberMessageShow = 0;
                gameData.numberMessage = 0;
                isReading = false;
                parent.repaint();
                break;
            case 2:
                isShowReply = true;
                if (replyBox == null) {
                    replyBox = new CustomTextBox(PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, false, keyPad);
                    replyBox.setShowTypeInput(false);
                }
                replyBox.setText("");
                parent.repaint();
                //tra loi
                break;
        }
    }

    protected void doUserInfo() {
        isShowSubMenu = false;
        parent.repaint();
        switch (subMenu.getSelectedIndex()) {
            case 0:
                //status
                isShowReply = true;
                if (replyBox == null) {
                    replyBox = new CustomTextBox(PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, false, keyPad);
                    replyBox.setShowTypeInput(false);
                }
                replyBox.setText("");
                parent.repaint();
                break;
            case 1:
                // de nghi sinh con
                if (gameData.userInfoShow.getGender() == 0) {
                    dialogString = "Đã gửi yêu cầu cho vợ bạn";
                } else {
                    dialogString = "Đã gửi yêu cầu cho chồng bạn";
                }
                parent.repaint();
                Request.sendMessage(MESSAGE_BIRTH, gameData.userInfoShow.getMatName(), "0");
                break;
        }
    }

    public void dispose() {
        dialogString = null;
        //-------------------------------------------
        loadingImage = null;
//        backgroundImage = null;
        newMessageImage = null;
        tabImage = null;
        transparentBackground = null;
        //------------------------------------------
        subMenu = null;
        library = null;
        parent = null;
        gameData = null;
        Debug.d("dipose");

        Runtime.getRuntime().gc();
    }

    protected void getMyinfo() {
        //thong tin ca nhan
        isGetUserInfo = true;
        Request.getUserInfo();
        isLoading = true;
        parent.repaint();
    }

    protected void getMyMessage() {
        //hop thu
        if (gameData.userMessage == null) {
            Request.getListMessage();
            isLoading = true;
        } else {
            numberMessage = gameData.numberMessage;
            numberMessageShow = numberMessage < messageBoardHeight / ROW_HEIGHT ? numberMessage : messageBoardHeight / ROW_HEIGHT;
            isShowMessage = true;
            gameData.isHasNewMessage = false;
            saveSubMenu = subMenu.getList();
            subMenu.setList(new String[]{"Xóa", "Xóa hết", "Trả lời"});
            messageIndex = 0;
        }
        parent.repaint();
    }

    protected void showSpeaker() {
        isShowSubMenu = false;
        isShowSpeaker = true;
        speakerBox.setText("");
        parent.repaint();
    }

    protected void inputSpeaker(int key) {
        String text = speakerBox.getText();
        if (text.length() < 25) {
            speakerBox.inputChar(key);
        } else {
            dialogString = "Không dùng quá 25 ký tự";
        }
        parent.repaint();
    }

//----------------------------------------------------------------------------------
    protected void showShopScene() {
        parent.switchScene(MainCanvas.SHOP_SCENE);
    }

    protected void showChooseGame() {
        parent.switchScene(MainCanvas.CHOOSE_GAME_SCENE);
    }

    protected void showUserScene() {
        parent.switchScene(MainCanvas.USER_INFO_SCENE);

    }

    protected void showChooseRoom() {
        parent.switchScene(MainCanvas.CHOOSE_ROOM_SCENE);
    }

    protected void showChooseTable() {
        parent.switchScene(MainCanvas.CHOOSE_TABLE_SCENE);
    }

    protected void showLogin() {
        parent.switchScene(MainCanvas.LOGIN_SCENE);
    }

    protected void showWaitingRoom() {
        parent.switchScene(MainCanvas.WAITING_SCENE);
    }

    protected void showTienlenScene() {
        parent.switchScene(MainCanvas.TIEN_LEN_SCENE);
    }

    protected void showTopUser() {
        parent.switchScene(MainCanvas.TOP_USER_SCENE);
    }

    protected void showListFriend() {
        parent.switchScene(MainCanvas.LIST_FRIEND_SCENE);
    }

    protected void showPhomScene() {
        parent.switchScene(MainCanvas.PHOM_SCENE);
    }

    protected void showChessScene() {
        parent.switchScene(MainCanvas.CO_VUA_SCENE);
    }

    protected void showBacayScene() {
        parent.switchScene(MainCanvas.BA_CAY_SCENE);
    }

    protected void showListMessage() {
        parent.switchScene(MainCanvas.LIST_MESSAGE);
    }

    void onSizeChange(int w, int h) {
        screenMode = (w < h ? SCREEN_PORTRAIT : SCREEN_LANDSCAPE);
        checkType();
    }
}
